<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function Person(name) {
        this.name = name;
      }
      Person.prototype.fn = function () {
        console.log("fn");
      };
      function myNew(fn, name) {
        //  创建一个空的简单JavaScript对象（即{}）；
        let obj = {};
        // 为步骤1新创建的对象添加属性__proto__，将该属性链接至构造函数的原型对象
        obj.__proto__ = fn.prototype;
        //  将步骤1新创建的对象作为this的上下文
        fn.call(obj, name);
        // 如果该函数没有返回对象，则返回this。
        return obj;
      }
      let zhangsan = myNew(Person, "张三");
      console.log(zhangsan.name);
      zhangsan.fn();
    </script>
  </body>
</html>
